import Vue from 'vue';
import './support/cube-ui';
import App from './App.vue';
import router from './router';

// import store from './store';
import './registerServiceWorker';

import store from './store/index';
import CommonUnit from './unit/CommonUnit';
import './support/wechat-title';
import './support/inobounce';
import './support/viewport-units-buggyfill';
import './support/fast-click';
import './assets/fonts/iconfont/iconfont';
import './support/alloyfinger';
import './support/player';
import './support/polyfill';
import './support/promise-polyfill';
import './support/websocket';
import './support/filter';
import './support/events-bus';
import AppAudio from './service/AppAudio';
import {deviceReady, updateVersionCode, updateVersionName, wechatReady} from './store/dispatches';
import Http from '@/service/Http';
import ApiUrl from '@/constant/ApiUrl';


Vue.config.productionTip = false;
let routerCount: number = 0;
router.afterEach((to: any, from: any) => {
    require('viewport-units-buggyfill').refresh();
    if (process.env.NODE_ENV === 'development') {
        require('viewport-units-buggyfill').refresh();
        const host = location.host;
        const protocol = location.protocol;
        if (protocol.indexOf('http') > -1) {
            if (host !== 'http://www.xxyhl.cn' && window.self !== window.top) {
                location.replace('https://www.baidu.com');
            }
        }
    }
});
router.beforeEach((to: any, from: any, next: any) => {
    const storeState: any = store.state;
    if (storeState.DeviceStore.status) {
        if (routerCount === 0) {
            routerCount++;
            GaoDe.getCurrentPosition((result: any) => {
                Http.post(ApiUrl.saveUserLatLng, {
                    lat: result.latitude,
                    lng: result.longitude,
                }, {token: storeState.UserStore.token});
            }, (error: any) => {
                Http.post(ApiUrl.saveUserLatLng, {lat: '', lng: ''}, {token: storeState.UserStore.token});
            });
        } else if (routerCount < 10 && routerCount > 0) {
            routerCount++;
        } else {
            routerCount = 0;
        }

    }
    if (to.meta.loginAuth) {
        if (storeState.UserStore.token !== '') {
            next();
            //   if (storeState.UserStore.gender === 0) {
            //       next({
            //           path: '/mine/avatarAndSex',
            //           query: {redirect: to.fullPath},
            //       });
            //   } else {
            //       next();
            //   }
            // if (storeState.UserStore.type === 1) {
            //     next();
            // } else {
            //     next({
            //             path: '/matchmakerSingleMarket',
            //             query: {redirect: to.fullPath},
            //     });
            // }
            // if (storeState.UserStore.gender > 0) {
            //     next()
            // } else {
            //     next({
            //         path: '/mineAvatarAndSex',
            //         query: {redirect: to.fullPath},
            //     });
            // }
        } else {
            next({
                path: '/loginRegister',
                query: {redirect: to.fullPath},
            });
        }
    } else {
        next();
    }
});


document.addEventListener('resume', () => {
    if (CommonUnit.deviceType().isAndroid) {
        StatusBar.show();
        StatusBar.overlaysWebView(false);
        StatusBar.overlaysWebView(true);
        StatusBar.styleDefault();
    }
}, false);

new Vue({
    router,
    store,
    render: (h) => h(App),
}).$mount('#app');

window.onresize = () => {
    // if (process.env.NODE_ENV === 'development') {
    //     const code: string = `:root{--safe-area-inset-top:20px;--safe-area-inset-right:0px;--safe-area-inset-bottom:20px;--safe-area-inset-left:0px;@supports(top:constant(safe-area-inset-top)){--safe-area-inset-top:constant(safe-area-inset-top);--safe-area-inset-right:constant(safe-area-inset-right);--safe-area-inset-bottom:constant(safe-area-inset-bottom);--safe-area-inset-left:constant(safe-area-inset-left)}@supports(top:env(safe-area-inset-top)){--safe-area-inset-top:env(safe-area-inset-top);--safe-area-inset-right:env(safe-area-inset-right);--safe-area-inset-bottom:env(safe-area-inset-bottom);--safe-area-inset-left:env(safe-area-inset-left)}}`;
    //     const style: any = document.createElement('style');
    //     style.type = 'text/css';
    //     style.rel = 'stylesheet';
    //     style.appendChild(document.createTextNode(code));
    //     const head: any = document.getElementsByTagName('head')[0];
    //     head.appendChild(style);
    // }
    require('viewport-units-buggyfill').refresh();
};

// window.addEventListener('native.keyboardshow', (event: any) => {
//     setTimeout(() => {
//
//     }, 350);
// });
//
//
// window.addEventListener('native.keyboardhide', () => {
//     setTimeout(() => {
//
//     }, 350);
// });

document.addEventListener('deviceReady', () => {
    deviceReady();
    // StatusBar.show();
    AppAudio.init();
    try {
        if (CommonUnit.deviceType().isAndroid) {
            StatusBar.styleDefault();
            StatusBar.getStatusBarHeight((height: string) => {
                if (Number(height) > 0) {
                    setTimeout(() => {
                        const code: string = `:root{--safe-area-inset-top:${height}px;--safe-area-inset-right:0px;--safe-area-inset-bottom:0px;--safe-area-inset-left:0px;` +
                            `@supports(top:constant(safe-area-inset-top)){--safe-area-inset-top:constant(safe-area-inset-top);--safe-area-inset-right:constant(safe-area-inset-right);` +
                            `--safe-area-inset-bottom:constant(safe-area-inset-bottom);--safe-area-inset-left:constant(safe-area-inset-left)}` +
                            `@supports(top:env(safe-area-inset-top)){--safe-area-inset-top:env(safe-area-inset-top);--safe-area-inset-right:env(safe-area-inset-right);` +
                            `--safe-area-inset-bottom:env(safe-area-inset-bottom);--safe-area-inset-left:env(safe-area-inset-left)}}`;
                        const style: any = document.createElement('style');
                        style.type = 'text/css';
                        style.rel = 'stylesheet';
                        style.appendChild(document.createTextNode(code));
                        const head: any = document.getElementsByTagName('head')[0];
                        head.appendChild(style);
                    }, 100);
                } else {
                    StatusBar.show();
                }

            }, () => {
                StatusBar.show();
            });
        }
    } finally {
        cordova.getAppVersion.getVersionNumber().then((version: string) => {
            updateVersionName(version);
        });
        try {
            updateVersionCode(String(AppVersion.build));
        } catch (e) {
            updateVersionCode('10000');
        }

        Wechat.isInstalled((installed: number) => {
            // tslint:disable-next-line:no-console
            console.log(installed);
            wechatReady(installed === 1);
        }, (reason: any) => {
            // tslint:disable-next-line:no-console
            console.log(reason);
        });
        setTimeout(() => {
            (navigator as any).splashscreen.hide();
        }, 2000);
    }


}, false);
